﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.IO;
using System.Web;
using System.Threading.Tasks;
using System.Data;
using BartonExtension;
using ResponseObject;
using SQLWrapper;
using Model.App;
using Model.Images;
using Model.Weixin;
using Services.Common;
using Services.Group;

namespace iShangMai.Controllers.App
{
    public class GroupController : ApiController
    {
        [HttpPost]
        public async Task<ZKResponse> CreateGroup(ISMCreateGroupRequest request)
        {
            ZKResponse response = new ZKResponse();
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.CREATE_GROUP,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> GetMyGroups(UInt64 uid,int page,int pageSize,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMGetGroupRequest request = new ISMGetGroupRequest()
            {
                uid = uid,
                page = page,
                pageSize = pageSize,
                token = token
            };
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.GET_GROUP_LIST,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }

        [HttpGet]
        public async Task<ZKResponse> GetGroupInfo(UInt64 uid, UInt64 group_id, string token)
        {
            ZKResponse response = new ZKResponse();
            ISMGetGroupInfoRequest request = new ISMGetGroupInfoRequest()
            {
                uid = uid,
                group_id = group_id,
                token = token
            };
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.GET_GROUP_INFO,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> JoinGroup(UInt64 uid, UInt64 group_id, string content,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMJoinGroupRequest request = new ISMJoinGroupRequest()
            {
                uid = uid,
                group_id = group_id,
                content = content,
                token = token
            };
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.JOIN_GROUP,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> ExecuteJoinRequest(UInt64 uid, UInt64 group_id, GroupRequestStatus status, string token)
        {
            ZKResponse response = new ZKResponse();
            ISMExecuteJoinRequest request = new ISMExecuteJoinRequest()
            {
                uid = uid,
                group_id = group_id,
                status = status,
                token = token
            };
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.PROCESS_JOIN_REQUEST,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> SendMessageToGroup(UInt64 share_id, string group_id,UInt64 uid,string content,GroupMessageType type,string images, string token)
        {
            ZKResponse response = new ZKResponse();
            ISMCreateGroupMessageRequest request = new ISMCreateGroupMessageRequest()
            {
                share_id = share_id,
                group_id = group_id,
                content = content,
                uid = uid,
                type = type,
                image_ids = images,
                token = token
            };
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.SEND_MESSAGE_TO_GROUP,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> GetGroupMessages(UInt64 group_id,int page,int pageSize,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMGetGroupMessageRequest request = new ISMGetGroupMessageRequest()
            {
                group_id = group_id,
                page = page,
                pageSize = pageSize,
                token = token
            };
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.GET_GROUP_MESSAGES,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        [HttpGet]
        public async Task<ZKResponse> GetSingleGroupMessage(UInt64 group_message_id,string token)
        {
            ZKResponse response = new ZKResponse();
            ISMGetSingleGroupMessageRequest request = new ISMGetSingleGroupMessageRequest()
            {
                group_message_id = group_message_id,
                token = token
            };
            var provider = (ISMGroupServiceProvider)ISMServiceStack.sharedInstance[ISMServiceStackKeys.GROUP];
            ISMServiceModel model = new ISMServiceModel()
            {
                operationIdentifier = ISMGroupServiceProvider.GET_SINGLE_GROUP_MESSAGE,
                userinfo = request
            };
            var result = await provider.doProcess(model);
            response.isProcessSuccess = result.isOperationSuccess;
            response.errMsg = result.errorMsg;
            response.userinfo = result.dbReturningState;
            return response;
        }
        
    }
}
